package com.micangcaihang.micang.mvp.task.presenter;

import com.micangcaihang.common.baserx.RxSchedulers;
import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.RxHelper;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.micang.entity.FTaskCommonOwnerInfoEntity;
import com.micangcaihang.micang.entity.MatesEntity;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.mvp.task.contract.FTaskCommonOwnerInfoContract;
import com.micangcaihang.micang.mvp.task.model.FTaskCommonOwnerInfoModel;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/17
 *     desc   : 风控-共有人信息
 *     version: 1.0
 * </pre>
 */

public class FTaskCommonOwnerInfoPresenter extends FTaskCommonOwnerInfoContract.Presenter {
    private String id;
    private FTaskCommonOwnerInfoModel mModel;
    private boolean isRefresh;

    @Override
    public void setV(FTaskCommonOwnerInfoContract.View v) {
        super.setV(v);
        mModel = new FTaskCommonOwnerInfoModel();
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     * 每次进入界面获取网络数据，更新界面
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    @Override
    public void loadFirstData(boolean isRefresh) {
        this.isRefresh = isRefresh;
        loadCommonOwnerInfoData();
    }

    /**
     * 加载更多数据
     */
    @Override
    public void loadMoreData() {

    }

    /**
     * 加载下拉框数据
     *
     * @param type
     * @param title
     */
    @Override
    public void loadPickerViewData(String type, String title) {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getPickerViewData(type);
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnPickerViewData(listApiResult.data, title);
                } else {
                    mView.showTip("暂时没有" + title + "信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 加载共有人信息数据
     */
    @Override
    public void loadCommonOwnerInfoData() {
        Observable<ApiResult<List<FTaskCommonOwnerInfoEntity>>> observable = mModel.getCommonOwnerInfoData(id, "2");
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<FTaskCommonOwnerInfoEntity>>>() {

            @Override
            protected void doOnStart() {
                mView.showLoading();
            }

            @Override
            protected void doOnNext(ApiResult<List<FTaskCommonOwnerInfoEntity>> result) {
                setCommonOwnerInfoData(result);
                mView.returnCommonOwnerInfoData(result.data);
            }

            @Override
            protected void doOnComplete() {
                mView.showContent();
            }

            @Override
            protected void doOnError(ApiException e) {
//                super.doOnError(e);
                mView.showNetError(e);
            }
        }));
    }

    /**
     * 删除用户
     *
     * @param customer_id
     */
    @Override
    public void deleteUser(String customer_id) {
        Observable<ApiResult<Void>> user = mModel.deleteUser(customer_id);
        mRxManage.add(user.subscribeWith(new MiSubScriber<ApiResult<Void>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<Void> voidApiResult) {
                mView.showTip("删除成功");
                mView.onDeleteSucceed();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }
        }));
    }

    private void setCommonOwnerInfoData(ApiResult<List<FTaskCommonOwnerInfoEntity>> result) {
        if (result.data != null && result.data.size() > 0) {
            for (int i = 0; i < result.data.size(); i++) {
                FTaskCommonOwnerInfoEntity entity = result.data.get(i);
                List<MatesEntity> mates = result.data.get(i).getMates();
                boolean isHaveNew = false;
                boolean isHaveOld = false;
                boolean isHanceWid = false;
                switch (entity.getMarriage_status()) {
                    case "2":
                        //循环遍历现任配偶,如果已经有不做任何操作
                        //如果没有重新new一个添加进去
                        for (MatesEntity matesEntity : mates) {
                            if ("1".equals(matesEntity.getRelationship_type())) {
                                isHaveNew = true;
                                break;
                            }
                        }
                        if (!isHaveNew) {
                            MatesEntity Current = new MatesEntity("1");
                            mates.add(Current);
                        }
                        break;
                    case "3":
                        //循环遍历现任配偶,如果已经有不做任何操作
                        //如果没有重新new一个添加进去
                        for (MatesEntity matesEntity : mates) {
                            if ("1".equals(matesEntity.getRelationship_type())) {
                                isHaveNew = true;
                                break;
                            }
                        }
                        if (!isHaveNew) {
                            MatesEntity Current = new MatesEntity("1");
                            mates.add(Current);
                        }
                        //循环遍历前任配偶,如果已经有不做任何操作
                        //如果没有重新new一个添加进去
                        for (MatesEntity matesEntity : mates) {
                            if ("2".equals(matesEntity.getRelationship_type())) {
                                isHaveOld = true;
                                break;
                            }
                        }
                        if (!isHaveOld) {
                            MatesEntity Current = new MatesEntity("2");
                            mates.add(Current);
                        }
                        break;
                    case "4":
                        //循环遍历前任配偶,如果已经有不做任何操作
                        //如果没有重新new一个添加进去
                        for (MatesEntity matesEntity : mates) {
                            if ("2".equals(matesEntity.getRelationship_type())) {
                                isHaveOld = true;
                                break;
                            }
                        }
                        if (!isHaveOld) {
                            MatesEntity Current = new MatesEntity("2");
                            mates.add(Current);
                        }
                        break;
                    case "5":
                        //循环遍历前任配偶,如果已经有不做任何操作
                        //如果没有重新new一个添加进去
                        for (MatesEntity matesEntity : mates) {
                            if ("3".equals(matesEntity.getRelationship_type())) {
                                isHanceWid = true;
                                break;
                            }
                        }
                        if (!isHanceWid) {
                            MatesEntity Current = new MatesEntity("3");
                            mates.add(Current);
                        }
                        break;
                    default:
                        break;
                }
            }
        } else {
            if (result.data == null) {
                result.data = new ArrayList<>();
            }
            FTaskCommonOwnerInfoEntity entity = new FTaskCommonOwnerInfoEntity();
            List<MatesEntity> list = new ArrayList<>();
            MatesEntity matesEntity = new MatesEntity();
            list.add(matesEntity);
            entity.setMates(list);
            result.data.add(entity);
        }
    }

    /**
     * 保存提交保存信息
     *
     * @param requests
     */
    @Override
    public void saveCommonOwnerInfo(List<FTaskCommonOwnerInfoEntity> requests, String id) {
        Observable<ApiResult<Void>> observable = mModel.savaLoanerData(requests, id);
        mRxManage.add(observable.
                concatMap(new Function<ApiResult<Void>, ObservableSource<ApiResult<List<FTaskCommonOwnerInfoEntity>>>>() {
                    @Override
                    public ObservableSource<ApiResult<List<FTaskCommonOwnerInfoEntity>>> apply(@NonNull ApiResult<Void> voidApiResult) throws Exception {
                        return mModel.getCommonOwnerInfoData(id, "2");
                    }
                })
                .compose(RxSchedulers.observableIoToMain())
                .compose(RxHelper.handleResult())
                .subscribeWith(new MiSubScriber<ApiResult<List<FTaskCommonOwnerInfoEntity>>>() {

                    @Override
                    protected void doOnStart() {
                        mView.showDialogLoading("");
                    }

                    @Override
                    protected void doOnNext(ApiResult<List<FTaskCommonOwnerInfoEntity>> result) {
                        setCommonOwnerInfoData(result);
                        mView.returnCommonOwnerInfoData(result.data);
                        mView.showTip("保存成功");
                    }

                    @Override
                    protected void doOnComplete() {
                        mView.hideDialogLoading();
                    }

                    @Override
                    protected void doOnError(ApiException e) {
                        super.doOnError(e);
                        mView.hideDialogLoading();
                    }
                }));
    }
}
